package sz2.logger;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 使用注解定义切面
 */
@Aspect
@Component
public class UserServiceLogger {
    private static final Logger logger = Logger.getLogger(UserServiceLogger.class);

    /**
     * 前置增强
     */
    @Before("execution(* sz2.service..*.save(sz2.pojo.User))")
    public void before(JoinPoint jp){
        logger.info("【前置增强】调用" + jp.getTarget() + "的" + jp.getSignature().getName() + "方法。方法入参：" + Arrays.toString(jp.getArgs()) + "\n");
    }

    /**
     * 后置增强
     */
    @AfterReturning(pointcut = "execution(* org.example.service..*.save(org.example.pojo.User))",returning = "returnValue")
    public void after(JoinPoint jp,Object returnValue){
        logger.info("【后置增强】调用" + jp.getTarget() + "的" + jp.getSignature().getName() + "方法。方法入参：" + Arrays.toString(jp.getArgs())+ "\n");
    }
}
